/**
 * @author:xieguojun
 * @see 1.0
 */

//失败重新验证地址
 
//重新验证间隔数(ms)
var __TRY_ON_FAILURE_INTERVAL__ = 5 * 1000;

//重新验证次数
var __TRY_ON_FAILURE_COUNT__ = 4;

var __FORM_TIMEOUT__= 1000;

var __LOADMASK__ = null;

var __WSSIP_GENERATE_GUID__ = function(){
	Wssip.util.request(__GUID_GENERATE_URL__, {}, "POST", 
		function(form, action) {
			__GUID__ = Wssip.form.getFormValue(action,'guid');
		}, 
		function(form, action) {
			Wonders.Msg.info('处理失败，请刷新当前页面或重新登录。','提示');
	});
}

/**
 * 获取业务办理状态
 * @param icount 当前尝试次数
 * @param guid 流水号
 * @returns
 */
var __TRY_GET_ON_FAILURE__ = function(icount, guid) {
	Wssip.util.request(__TRY_ON_FAILURE_URL__, {
		'guid' : guid 
	}, "POST", function(form, action) {
		if(__LOADMASK__){
			__LOADMASK__.hide();
		}
		Wonders.Msg.info('业务办理成功！','提示',function(){__WSSIP_GENERATE_GUID__();});
	}, function(form, action) {
		if (action.response.status == 200) {
			//defaultFormExceptionHanlder(form,action);
			if(__LOADMASK__){
				__LOADMASK__.hide();
			}
			var errmsg = Ext.decode(action.response.responseText).errors[0].msg;
			Wonders.Msg.error(errmsg,'错误',function(){__WSSIP_GENERATE_GUID__();});
			return true;
		} else {
			if (icount < __TRY_ON_FAILURE_COUNT__) {
				window.setTimeout(function() {
					if(__LOADMASK__){
						__LOADMASK__.hide();
						__LOADMASK__.msg = "由于网络不稳定，未能正常获取业务办理状态，现尝试第[<b>" + (icount+1) + "</b>]次获取......";
						__LOADMASK__.show();
					}
					__TRY_GET_ON_FAILURE__(icount + 1, guid);
				}, __TRY_ON_FAILURE_INTERVAL__);
			}else{
				Ext.MessageBox.show({
				    title:"错误",
				    msg:"由于网络原因，未能正常获取到业务办理状态，请到业务日志中查询本业务是否办理成功。",
				    buttons:Ext.Msg.OK,
				    width:450,
				    icon:Ext.MessageBox.ERROR,
				    closable:true,
				    prompt:false,
				    multiline:false
				});
				__WSSIP_GENERATE_GUID__();
				if(__LOADMASK__){
					__LOADMASK__.hide();
				}
			}
			return true;
		}
	},null,__FORM_TIMEOUT__);
}

var __WSSIP_FAILURE_HANLDER__ = function(form, action) {
	if (action.response.status == 200) {
		//defaultFormExceptionHanlder(form,action);
		//__WSSIP_GENERATE_GUID__();
		var errmsg = Ext.decode(action.response.responseText).errors[0].msg;
		Wonders.Msg.error(errmsg,'错误',function(){__WSSIP_GENERATE_GUID__();});
		return true;
	} else if(action.response.status == 601){
		__MENUID__ = action.response.responseText.substr(action.response.responseText.indexOf("BusiType")+10,8);
		__SGUID__ = action.response.responseText.substr(action.response.responseText.indexOf("SGUID")+6,36);
		Ext.MessageBox.show({
		    title:"错误",
		    msg:"由于网络不稳定，系统响应超时，请刷新页面重新办理！MENUID="+__MENUID__+"<div></div>"+"[RGUID="+__GUID__+"]"+"<div></div>"+"[SGUID="+__SGUID__+"]",
		    buttons:Ext.Msg.OK,
		    width:350,
		    icon:Ext.MessageBox.ERROR,
		    closable:true,
		    prompt:false,
		    multiline:false
		});	
	}else{
		// 网络问题导致的处理
		__LOADMASK__= new Ext.LoadMask(document.body, {    
			    msg: "由于网络不稳定，未能正常获取业务办理状态，现尝试第[<b>1</b>]次获取......",
			    removeMask:true   
		});
		__LOADMASK__.show();
		__TRY_GET_ON_FAILURE__(1,__GUID__);
		return false;
	}
}
